package com.smooth.site.sx.pc.offer.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.dao.gen.BizOfferMapper;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizJobApplay;
import com.smooth.dao.gen.bean.BizOffer;
import com.smooth.dao.gen.bean.BizOfferExample;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.mag.biz.applyjob.service.BizJobApplayService;
import com.smooth.site.sx.mag.biz.comp.service.BizCompanyService;
import com.smooth.site.sx.mag.biz.offer.service.BizOfferService;
import com.smooth.site.sx.mag.biz.student.service.BizStudentService;
import com.smooth.site.sx.pc.job.enums.ApplyJobStatus;
import com.smooth.site.sx.pc.offer.enums.OfferStatus;
import com.smooth.sys.pc.vo.ListRsp;

@Component
public class PcOfferService {
	Logger log = LoggerFactory.getLogger(PcOfferService.class);
	@Autowired
	BizOfferMapper bizOfferMapper;
	@Autowired
	BizOfferService bizOfferService;
	@Autowired
	BizStudentService bizStudentService;
	@Autowired
	BizJobApplayService bizJobApplayService;
	@Autowired
	BizCompanyService bizCompanyService;
	public BizOffer createOffer(BizJobApplay bizJobApplay,String pdfUrl) {
		BizOffer bizOffer = new BizOffer();
		
		BizStudent bizStudent = bizStudentService.get(bizJobApplay.getStuId());
		Date date = new Date();
		bizOffer.setCompId(bizJobApplay.getCompId());
		bizOffer.setCompName(bizJobApplay.getCompName());
//		bizOffer.setComTime(comTime);
		bizOffer.setCreateDate(date);
		bizOffer.setJobId(bizJobApplay.getJobId());
		bizOffer.setJobName(bizJobApplay.getJobTitle());
		bizOffer.setApplyId(bizJobApplay.getId());
//		bizOffer.setOfferPdf(offerPdf);
		bizOffer.setOfferStatus(OfferStatus.toconfirm.value);
		bizOffer.setSchId(bizStudent.getSchId());
		bizOffer.setSchName(bizStudent.getSchName());
		bizOffer.setStuId(bizStudent.getId());
		bizOffer.setStuName(bizStudent.getStuName());
		bizOffer.setUpdateDate(date);
		bizOffer.setOfferPdf(pdfUrl);
		
		bizOffer = bizOfferService.save(bizOffer);
		
		bizJobApplay.setApplayStatus(ApplyJobStatus.offerCreated.value);
		if(!bizCompanyService.canPubJob(bizJobApplay.getCompId())) {
			throw new SiteException(SiteCodeEnums.zhiwei_notengouth, "情联系管理员 ");
		}
		bizJobApplayService.save(bizJobApplay);
		return bizOffer;
	}
	
	
	public BizOffer addAttachPdf(String offerId,String offerPdf) {
		BizOffer bizOffer = new BizOffer();
		bizOffer.setOfferPdf(offerPdf);
		bizOffer.setId(offerId);
		bizOffer = bizOfferService.save(bizOffer);
		return bizOffer;
	}
	@Transactional
	public BizOffer statusBizoffer(String offerId,OfferStatus offerStatus) {
		BizOffer bizOfferRecord = bizOfferService.get(offerId);
		
		OfferStatus offerDbStatus = OfferStatus.getStatus(bizOfferRecord.getOfferStatus());
		
		if((OfferStatus.confirmed.equals(offerStatus)&&OfferStatus.toconfirm.equals(offerDbStatus))||
				(OfferStatus.toconfirm.equals(offerStatus))||
				(OfferStatus.failed.equals(offerStatus))) {
			BizOffer bizOffer = new BizOffer();
			bizOffer.setOfferStatus(offerStatus.value);
			bizOffer.setId(offerId);
			bizOffer = bizOfferService.save(bizOffer);
			if((OfferStatus.confirmed.equals(offerStatus)&&OfferStatus.toconfirm.equals(offerDbStatus))) {
				String compId = bizOfferRecord.getCompId();
				bizCompanyService.subJobshenyu(compId);
			}
			
			return bizOffer;
		}else {
			throw new SiteException(SiteCodeEnums.param_error, offerId+"状态异常");
		}
	}
	
	
	public ListRsp<BizOffer> queryStuBizoffers(String stuId,Integer offerStatus) {
		
		ListRsp<BizOffer> orsp = new ListRsp<BizOffer>();
		BizOfferExample bizOfferExample = new BizOfferExample();
		
		BizOfferExample.Criteria critria = bizOfferExample.createCriteria();
		critria.andStuIdEqualTo(stuId);
		
		if(offerStatus!=null) {
			OfferStatus offerStatusEnum = OfferStatus.getStatus(offerStatus);
			critria.andOfferStatusEqualTo(offerStatusEnum.value);
		}else {
			List<Integer> ilist = new ArrayList<Integer>();
			ilist.add(OfferStatus.failed.value);
			ilist.add(OfferStatus.confirmed.value);
			ilist.add(OfferStatus.toconfirm.value);
			critria.andOfferStatusIn(ilist);
		}
		
		List<BizOffer> blist = bizOfferMapper.selectByExample(bizOfferExample);
		
		orsp.setDatalist(blist);
		orsp.setTsize(bizOfferMapper.countByExample(bizOfferExample));
		
		return orsp;
	}
	
	public ListRsp<BizOffer> queryCompBizoffers(String compId,OfferStatus offerStatus) {
		ListRsp<BizOffer> orsp = new ListRsp<BizOffer>();
		BizOfferExample bizOfferExample = new BizOfferExample();
		
		BizOfferExample.Criteria critria = bizOfferExample.createCriteria();
		critria.andCompIdEqualTo(compId);
		if(offerStatus!=null) {
			critria.andOfferStatusEqualTo(offerStatus.value);
		}
		
		List<BizOffer> blist = bizOfferMapper.selectByExample(bizOfferExample);
		
		orsp.setDatalist(blist);
		orsp.setTsize(bizOfferMapper.countByExample(bizOfferExample));
		
		return orsp;
	}
}
